import { getShopDetail } from "@src/api/booking";
import { storeBindingsBehavior } from 'mobx-miniprogram-bindings'
import base from "@src/store/base";
import location from "@src/store/location";
import router from "@src/router";

Component({
  behaviors: [storeBindingsBehavior, router.routerBehavior],
  storeBindings: [{
    store: base,
    fields: ['themeColor', 'themeTitleColor', 'perms'],
  }, {
    store: location,
    fields: ['latitude', 'longitude'],
    actions: ['getLocation']
  }],
  data: {
    shopId: null,
    navIndex: 2,
  },
  methods: {
    onLoad(options) {
      if (options.navIndex) {
        this.setData({ shopId: options.shopId, navIndex: Number(options.navIndex) })
      } else {
        this.setData({ shopId: options.shopId, navIndex: 1 })
      }
      wx.nextTick(() => {
        this.fetchData();
      })
    },
    handleChange(e) {
      this.setData({ navIndex: e.currentTarget.dataset.index })
    },
    fetchData: function () {
      let data = {
        latitude: this.data.latitude ? this.data.latitude : "",
        longitude: this.data.longitude ? this.data.longitude : "",
        shopId: this.data.shopId
      }
      return getShopDetail(data).then(res => {
        if (res.code === 0) {
          let data = res.data
          let supplyService = this.parseStr(data.supplyService)
          let supplyFacility = this.parseStr(data.supplyFacility)

          let supplyFacilityCheckListArr = []
          let supplyServiceCheckListArr = []
          let customerCheckListArr = []
          if (supplyService) {
            supplyService.forEach((item) => {
              if (item.checkList.length > 0) {
                if (item.label === '其它服务') {
                  customerCheckListArr.push(...item.checkList)
                } else {
                  supplyServiceCheckListArr.push(...item.checkList)
                }
              }
            })
          }
          if (supplyFacility) {
            supplyFacility.forEach((item) => {
              if (item.checkList.length > 0) {
                if (item.label === '其它设施') {
                  customerCheckListArr.push(...item.checkList)
                } else {
                  supplyFacilityCheckListArr.push(...item.checkList)
                }
              }
            })
          }
          let shopInfo = data.shopInfo
          if (shopInfo.distance) {
            shopInfo.distance = (shopInfo.distance / 1000).toFixed(2)
          }
          this.setData({
            shopInfo: shopInfo,
            imageList: data.imageList,
            description: data.description,
            supplyFacilityCheckListArr: supplyFacilityCheckListArr,
            supplyServiceCheckListArr: supplyServiceCheckListArr,
            customerCheckListArr: customerCheckListArr,
            supplyService: supplyService,
            fullDayRoomState: data.fullDayRoomState,
            hourRoomState: data.hourRoomState,
            ktvRoomState: data.ktvRoomState,
            bookingNotes: data.bookingNotes
          })
        }
      })
    },
    handleOpenMap(e) {
      let data = e.currentTarget.dataset.item
      wx.openLocation({
        latitude: data.latitude,
        longitude: data.longitude,
        name: data.name,
        address: data.address
      });
    },
    parseStr(jsonStr) {
      if (!jsonStr) {
        return null;
      }
      try {
        return JSON.parse(jsonStr)
      } catch (e) {
        console.info("解析异常", e, jsonStr)
      }
      return null;
    },
  }
})
